package exttable.formgenerator;

import dbutils.DbUtils;
import exttable.dbclasses.Group;
import exttable.dbclasses.Probationer;
import exttable.dbclasses.Visit;
import exttable.questionnaire.QuestionnaireService;
import utils.Localisation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ADMIN
 * Date: 02.11.2010
 * Time: 11:35:39
 * To change this template use File | Settings | File Templates.
 */
public class SelectGroupAndProbationerForm {
    private JFrame frame;
    private JPanel panel;
    private List<Group> groupsList = new ArrayList<Group>();
    private JComboBox groupBox;
    private static JComboBox probationerBox;
    private static DefaultComboBoxModel dfcbModelProbationer;
    private JComboBox visitBox;
    private static DefaultComboBoxModel dfcbModelVisit;
    private List<Visit> visitList = new ArrayList<Visit>();

    private JButton nextButton;

    public SelectGroupAndProbationerForm() {
        groupsList = StaticMethods.getAnalysGroupList();
        frame = new JFrame();
        panel = new JPanel();
        groupBox = new JComboBox(groupsList.toArray());
        groupBox.setPreferredSize(new Dimension(300, 20));
        groupBox.addActionListener(ListenerFactory.reloadProbationerList());
        panel.add(new JLabel(Localisation.getMessage("menu.group")));
        panel.add(groupBox);
        dfcbModelProbationer = new DefaultComboBoxModel();
        probationerBox = new JComboBox(dfcbModelProbationer);
        probationerBox.addActionListener(ListenerFactory.reloadVisitList());
        panel.add(new JLabel(Localisation.getMessage("menu.probationer")));
        panel.add(probationerBox);
        panel.add(new JLabel(Localisation.getMessage("form.visit")));
        dfcbModelVisit = new DefaultComboBoxModel();
        visitBox = new JComboBox(dfcbModelVisit);
        panel.add(visitBox);
        nextButton = new JButton(Localisation.getMessage("form.continue"));
        nextButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Probationer p = (Probationer) probationerBox.getSelectedItem();
                    Visit v = (Visit) visitBox.getSelectedItem();
                    new QuestionnaireService(p, v.getVisitId());
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
        panel.add(nextButton);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(520, 250);
        frame.setVisible(true);
    }


    public static List<Probationer> getProbationerListByGroup(int group_id) {
        /*
        This method returns List<Probationer> of specifical group
         */
        List<Probationer> toReturn = new ArrayList<Probationer>();
        try {
            Connection conn = DbUtils.getDBConnectionGeneral();
            PreparedStatement ps = conn.prepareStatement("select * from probationers where group_id=" + group_id);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Probationer probationer = new Probationer();
                probationer.setProbationer_id(Integer.parseInt(rs.getString("probationer_id")));
                probationer.setProbationer_code(rs.getString("probationer_code"));
                probationer.setGroup_id(Integer.parseInt(rs.getString("group_id")));
//                probationer.setVisitList(SelectGroupAndProbationerForm.getVisitList(probationer.getProbationer_id(), conn));
                toReturn.add(probationer);
            }
            conn.close();
        }
        catch (Exception exp) {
            exp.printStackTrace();
        }
        return toReturn;
    }

    public  static  void changeDFCBModel(JComboBox groupBox) {

        Group g = (Group) groupBox.getSelectedItem();
        dfcbModelProbationer.removeAllElements();
        List<Probationer> probationersList = SelectGroupAndProbationerForm.getProbationerListByGroup(g.getGroupId());
        for (Probationer aProbationersList : probationersList) {
            dfcbModelProbationer.addElement(aProbationersList);
        }
    }

    public static void changeVisitDFCBModel(JComboBox probationerBox) {
        if (probationerBox.getSelectedItem() != null) {
            Probationer p = (Probationer) probationerBox.getSelectedItem();
            dfcbModelVisit.removeAllElements();
            List<Visit> visitsList = SelectGroupAndProbationerForm.getVisitList(p.getProbationer_id());
            for (Visit aVisitsList : visitsList) {
                dfcbModelVisit.addElement(aVisitsList);
            }
        }


    }

    public static List<Visit> getVisitList(int probationer_id) {
        List<Visit> toReturn = new ArrayList<Visit>();
        try {
            Connection conn = DbUtils.getDBConnectionGeneral();
            PreparedStatement ps = conn.prepareStatement("SELECT dic_visits.vizit_id," +
                    "dic_visits.name_ru, " +
                    "dic_visits.name_en, " +
                    "dic_visits.name_kk " +
                    "FROM " +
                    "dic_visits " +
                    "WHERE dic_visits.vizit_id NOT IN( " +
                    "SELECT result_answers.vizit_id FROM result_answers " +
                    "WHERE result_answers.probationer_id=? " +
                    ")"+
                    " AND dic_visits.vizit_id < 182");
            ps.setInt(1, probationer_id);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Visit visit = new Visit();
                visit.setVisitId(Integer.parseInt(rs.getString("vizit_id")));
                visit.setNameRu(rs.getString("name_ru"));
                visit.setNameEn(rs.getString("name_en"));
                visit.setNameKz(rs.getString("name_kk"));
                toReturn.add(visit);
            }
            conn.close();
        }
        catch (Exception exp) {
            exp.printStackTrace();
        }
        return toReturn;
    }


}


